/**
 * 
 */
package com.roshan.web.security.handlers;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;

import com.roshan.web.security.exceptions.RoarInsufficientAuthenticationException;
import com.roshan.web.security.exceptions.UserDeactivatedException;

public class RoarAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {

    private String defaultFailureUrl;

    private String mfaRequestUrl;

    private String customerDeactivateUrl;

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
            AuthenticationException exception) throws IOException, ServletException {

        super.setDefaultFailureUrl(this.defaultFailureUrl);

        if (exception instanceof RoarInsufficientAuthenticationException) {
            RoarInsufficientAuthenticationException ex = (RoarInsufficientAuthenticationException) exception;
            super.setDefaultFailureUrl(this.mfaRequestUrl + ex.getUserName());

        }

        if (exception instanceof UserDeactivatedException) {
            super.setDefaultFailureUrl(this.customerDeactivateUrl);
        }

        super.onAuthenticationFailure(request, response, exception);
    }

    public String getDefaultFailureUrl() {
        return this.defaultFailureUrl;
    }

    @Override
    public void setDefaultFailureUrl(String defaultFailureUrl) {
        this.defaultFailureUrl = defaultFailureUrl;
    }

    public String getMfaRequestUrl() {
        return this.mfaRequestUrl;
    }

    public void setMfaRequestUrl(String mfaRequestUrl) {
        this.mfaRequestUrl = mfaRequestUrl;
    }

    public void setCustomerDeactivateUrl(String customerDeactivateUrl) {
        this.customerDeactivateUrl = customerDeactivateUrl;
    }

    public String getCustomerDeactivateUrl() {
        return this.customerDeactivateUrl;
    }

}
